<?php
namespace Manage\Controller;
use Think\Controller;
class IndexController extends Controller {

    public function export_count(){
        header("Content-type: text/html; charset=utf-8");
        $this->display();

    }

    public function temp(){
        $fy_model = D('PumpPump');
        $fys = $fy_model->select();
        $record_model = D('PumpPumpRecord');
        $open_ = array();
//        foreach($fys as $fy){
//            $fy_status = $record_model->get_last_one_record($fy['id']);
//            $fy_status = $fy_status['status'];
//            $del_map['pumppumpid'] = array('eq', $fy['id']);
//            if($fy_status == 1){
//                $open_[] = $fy;
//                $del_map['id'] = array('eq', 1);
//                $record_model->where($del_map)->delete();
//            }
//            if($fy_status == 0 || $fy_status == -2){
//                $del_map['status'] = array('eq', 1);
//                $record_model->where($del_map)->delete();
//            }
//
//        }
        $str = "status >= 0 AND ( pump_start<='2016-10-21 12:59:59' and (pump_off>='2016-10-21 12:00:00' or pump_off is null) ) ";
        $map['status'] = array('egt', 0);
        $records = $record_model->where($map)->where($str)->select();
        $is = array();
        foreach($records as $record){
            $is[$record['pumppumpid']][] = $record;
            $fy_status = $record_model->get_last_one_record($record['pumppumpid']);
            $obj = $fy_model->queryObj($record['pumppumpid'], 'id');
            if($fy_status['status'] != 1){
                $del_map['pumppumpid'] = array('eq', $record['pumppumpid']);
                $record_model->where($del_map)->delete();
                dump($fy_status['status']);
            }
        }
//        dump($is);
//        foreach($is as $t_id => $temp_records){
//            if(count($temp_records)>1){
//                $del_map['pumppumpid'] = array('eq', $t_id);
//                $del_map['status'] = array('eq', 1);
//                $record_model->where($del_map)->delete();
//                $i++;
//            }
//        }
//        dump($i);
    }

    /**
     * 导出日报表
     */
    public function get_day(){
        header("Content-type: text/html; charset=utf-8");

        $this->display();
    }

    /**
     * 导出月报表
     */
    public function get_month(){
        header("Content-type: text/html; charset=utf-8");

        $this->display();
    }

    public function xman(){
        header("Content-type: text/html; charset=utf-8");
        $filename = './importexcel/sta.xls';
        $start_row =2;
        import("Org.Util.PHPExcel");
        import("Org.Util.PHPExcel.Reader.Excel5");
        $php_reader = new \PHPExcel_Reader_Excel5();
        $filename = file_zh_cn_icvon($filename);
        $php_excel = $php_reader->load($filename);

        $station_model = D('PumpingStation');
        $excel_stations = array();
        $no_in_db_station = array();
        for($sheet_index=0;$sheet_index<1;$sheet_index++){
            $currentSheet = $php_excel->getSheet($sheet_index);
            $allRow = $currentSheet->getHighestRow();
            for($currentRow = $start_row;$currentRow <= $allRow;$currentRow++){
                $station_name = $currentSheet->getCell('A'.$currentRow)->getValue();
                $one = $station_model->queryObj($station_name, 'ps_name');
                if(!$one){
                    $no_in_db_station[] = $station_name;
                }else{
                    $one['dev_ipc'] = $currentSheet->getCell('B'.$currentRow)->getValue();
                    $one['ps_guard_typ'] = $currentSheet->getCell('C'.$currentRow)->getValue();
//                    $station_model->save($one);
                }
            }
        }
        dump($no_in_db_station);
        ob_flush();
        flush();
    }

    public function cad_color_index(){
        header("Content-type: text/html; charset=utf-8");
        $filename = './excelfile/cad_color_index.xls';
        $start_row = 2;
        import("Org.Util.PHPExcel");
        import("Org.Util.PHPExcel.Reader.Excel5");
        $php_reader = new \PHPExcel_Reader_Excel5();
        $filename = file_zh_cn_icvon($filename);
        $php_excel = $php_reader->load($filename);

        $station_model = D('Csdt');
        $pump_model = D('CsdtPump');

        for($sheet_index=0;$sheet_index<$php_excel->getSheetCount();$sheet_index++){
            $currentSheet = $php_excel->getSheet($sheet_index);
            $allRow = $currentSheet->getHighestRow();
            $station_id = -1;
            $station_name = '';
            $temp = '';
            for($currentRow = $start_row;$currentRow <= $allRow;$currentRow++){
                $station_name = $currentSheet->getCell('B'.$currentRow)->getValue();
                if($station_name!=''){
                    $map['csdt_name'] = array('eq', $station_name);
                    $temp = $station_model->where($map)->find();
                    $station_id = $temp['id'];
                }
                //取得泵机数据
                $data = array();
                $data['temp'] = $temp['csdt_name'];
                $data['csdtid'] = $station_id;
                $data['csdt_number'] = $currentSheet->getCell('C'.$currentRow)->getValue();
                $data['csdt_name'] = $currentSheet->getCell('C'.$currentRow)->getValue();
//                if(is_numeric($currentSheet->getCell('E'.$currentRow)->getValue()))
//                    $data['pump_flow'] = $currentSheet->getCell('E'.$currentRow)->getValue();
                $data['csdt_flow'] = $currentSheet->getCell('D'.$currentRow)->getValue();
                dump($data);
                $pump_model->add($data);
                ob_flush();
                flush();
            }
        }
    }
}